package org.arch_learn.flashboot.context;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GlobalContext {
    public static Map<String, Object> globalSingletonPool;

    public static String starterPackage = null;

    public static void init() {
        globalSingletonPool = new HashMap<>();
    }

    public Map<String, Object> getGlobalSingletonPool() {
        return globalSingletonPool;
    }

    public static String getStarterPackage() {
        return starterPackage;
    }

    public static <T> T getInstance(Class<T> tClass) {
        String name = tClass.getName();
        return (T) (globalSingletonPool.get(name) == null ? globalSingletonPool.values().stream()
                .filter(v -> tClass.isInstance(v)).findFirst().get() : globalSingletonPool.get(name));
    }

    public static <T> List<T> getInstances(Class<T> tClass) {
        return globalSingletonPool.values().stream()
                .filter(v -> tClass.isInstance(v))
                .map(v -> (T) v).collect(Collectors.toList());
    }
}
